﻿Imports System.Data
Imports System.Data.OleDb
Imports System.Drawing.Printing

Public Class BusquedaClientesAnimales

    Public conexion As New OleDb.OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0; Data Source=nuevozoo.accdb")
    Public Shared eleccion As String

    'para animales
    Public adaptador As New OleDbDataAdapter("SELECT animales.nombre, clientes.nombre as Expr1, animales.cod_animal, animales.dueño, animales.veterinario, animales.especie, animales.raza FROM (animales INNER JOIN clientes ON animales.dueño = clientes.cod_cliente)", conexion)
    Public miDataset As New DataSet

    'para clientes
    Public adaptador2 As New OleDbDataAdapter("select clientes.cod_cliente, clientes.nombre, clientes.dni, clientes.direccion, clientes.telefono, clientes.observaciones from clientes", conexion)
    Public miDataset2 As New DataSet

    Public animal As Integer

    Private Sub BusquedaClientesAnimales_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        lab_Usuario.Text = Bienvenida.usuario

        'ANIMALES

        'rellenar datagriview
        adaptador.Fill(miDataset, "animales")
        DataGridViewAnimales.DataSource = miDataset
        DataGridViewAnimales.DataMember = "animales"

        'cambiar tamaño de las columnas
        DataGridViewAnimales.Columns("Expr1").Width = 200
        DataGridViewAnimales.Columns("nombre").Width = 100
        'cambiar nombre de las cabeceras de la tabla
        DataGridViewAnimales.Columns("nombre").HeaderText = "Animal"
        DataGridViewAnimales.Columns("Expr1").HeaderText = "Dueño"

        'ocultar columnas para que solo se vean el nombre del animal y del dueño
        DataGridViewAnimales.Columns("cod_animal").Visible = False
        DataGridViewAnimales.Columns("dueño").Visible = False

        'CLIENTES

        'rellenar datagriview
        adaptador2.Fill(miDataset2, "clientes")
        DataGridViewClientes.DataSource = miDataset2
        DataGridViewClientes.DataMember = "clientes"

        'cambiar tamaño de las columnas
        DataGridViewClientes.Columns("nombre").Width = 200
        DataGridViewClientes.Columns("dni").Width = 100

        'ocultar columnas para que solo se vean el nombre del animal y del dueño
        DataGridViewClientes.Columns("cod_cliente").Visible = False


    End Sub

    Private Sub text_nombrecli_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles text_nombrecli.TextChanged
        If text_nombrecli.Text = "" Then
            text_dni.Enabled = True
        Else
            text_dni.Enabled = False
        End If

        Dim comando As New OleDbCommand("select * from clientes where nombre like '%" & text_nombrecli.Text & "%'", conexion)
        adaptador2.SelectCommand = comando
        miDataset2.Clear()
        adaptador2.Fill(miDataset2, "clientes")
        DataGridViewClientes.DataSource = miDataset2
    End Sub

    Private Sub text_dni_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles text_dni.TextChanged
        If text_dni.Text = "" Then
            text_nombrecli.Enabled = True
        Else
            text_nombrecli.Enabled = False
        End If

        Dim comando As New OleDbCommand("select * from clientes where dni like '%" & text_dni.Text & "%'", conexion)

        adaptador2.SelectCommand = comando
        miDataset2.Clear()
        adaptador2.Fill(miDataset2, "clientes")
        DataGridViewClientes.DataSource = miDataset2
    End Sub

    Private Sub text_nombrean_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles text_nombrean.TextChanged
        If text_nombrean.Text = "" Then
            text_clientean.Enabled = True
        Else
            text_clientean.Enabled = False
        End If

        Dim comando As New OleDbCommand("SELECT animales.nombre, clientes.nombre as Expr1,animales.cod_animal, animales.veterinario, animales.especie, animales.raza FROM (animales INNER JOIN clientes ON animales.dueño = clientes.cod_cliente) where animales.nombre like '%" & text_nombrean.Text & "%'", conexion)
        adaptador.SelectCommand = comando
        miDataset.Clear()
        adaptador.Fill(miDataset, "animales")
        DataGridViewAnimales.DataSource = miDataset
        DataGridViewAnimales.Columns("cod_animal").Visible = False

    End Sub

    Private Sub text_clientean_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles text_clientean.TextChanged
        If text_clientean.Text = "" Then
            text_nombrean.Enabled = True
        Else
            text_nombrean.Enabled = False
        End If
        Dim comando As New OleDbCommand("SELECT animales.nombre, clientes.nombre as Expr1,animales.cod_animal,animales.veterinario, animales.especie, animales.raza from (animales INNER JOIN clientes ON animales.dueño = clientes.cod_cliente) where clientes.nombre like '%" & text_clientean.Text & "%'", conexion)
        adaptador.SelectCommand = comando
        miDataset.Clear()
        adaptador.Fill(miDataset, "animales")
        DataGridViewAnimales.DataSource = miDataset
        DataGridViewAnimales.Columns("cod_animal").Visible = False
    End Sub

    Private Sub DataGridViewAnimales_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridViewAnimales.CellContentClick
        PedirCitaToolStripMenuItem1.Enabled = True
        b_Detalles.Enabled = True
        ConsultaVentaToolStripMenuItem1.Enabled = True
    End Sub

    Private Sub b_Detalles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b_Detalles.Click
        animal = DataGridViewAnimales.CurrentRow.Cells("cod_animal").Value
        DetallesAnimales.ShowDialog()
    End Sub

    Private Sub PedirCitaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PedirCitaToolStripMenuItem.Click
        If DataGridViewAnimales.CurrentRow.Cells("cod_animal").Value <> 0 Then
            animal = DataGridViewAnimales.CurrentRow.Cells("cod_animal").Value
        End If
        PedirCita.Show()
        Me.Close()
    End Sub

    Private Sub MostrarDetallesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MostrarDetallesToolStripMenuItem.Click
        animal = DataGridViewAnimales.CurrentRow.Cells("cod_animal").Value
        DetallesAnimales.ShowDialog()
    End Sub

    Private Sub link_Cerrar_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
        Bienvenida.usuario = ""
        Bienvenida.Show()
        Me.Close()
    End Sub


    Private Sub imprimirCarnet(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        'metodo para la impresion del carnet
        Dim miImagen As Image = Image.FromFile("..\..\Resources\carnetClaro.png")
        Dim rectImg As New Rectangle(80, 90, 380, 230)
        ev.Graphics.DrawImage(miImagen, rectImg)

        Dim cliente As String = DataGridViewClientes.CurrentRow.Cells.Item("nombre").Value
        Dim dni As String = DataGridViewClientes.CurrentRow.Cells.Item("dni").Value
        Dim direc As String = DataGridViewClientes.CurrentRow.Cells.Item("direccion").Value
        Dim tel As String = DataGridViewClientes.CurrentRow.Cells.Item("telefono").Value

        ev.Graphics.DrawString("Nuevo Zoo", New Font("Arial", 18, FontStyle.Bold), Brushes.Black, 250, 105)
        ev.Graphics.DrawString("C/ Escosura 19, 28015 Madrid", New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 200, 135)

        ev.Graphics.DrawString("Nombre: " & cliente, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 100, 180)
        ev.Graphics.DrawString("DNI: " & dni, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 100, 205)
        ev.Graphics.DrawString("Dirección: " & direc, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 100, 230)
        ev.Graphics.DrawString("Teléfono: " & tel, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 100, 255)

    End Sub

    Private Sub ImprimirCarnetToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImprimirCarnetToolStripMenuItem.Click
        AddHandler PrintDocument1.PrintPage, AddressOf Me.imprimirCarnet
        PrintDocument1.Print()
    End Sub

    Private Sub DataGridViewClientes_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridViewClientes.CellContentClick
        ImprimirCarnetToolStripMenuItem.Enabled = True
    End Sub


    Private Sub VolverToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VolverToolStripMenuItem.Click
        GestionGeneral.Show()
        Me.Close()
    End Sub

    Private Sub PedirCitaToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PedirCitaToolStripMenuItem1.Click
        If DataGridViewAnimales.CurrentRow.Cells("cod_animal").Value <> 0 Then
            animal = DataGridViewAnimales.CurrentRow.Cells("cod_animal").Value
        End If
        PedirCita.Show()
        Me.Close()
    End Sub

    Private Sub ConsultaVentaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConsultaVentaToolStripMenuItem1.Click
        If DataGridViewAnimales.CurrentRow.Cells("cod_animal").Value <> 0 Then
            animal = DataGridViewAnimales.CurrentRow.Cells("cod_animal").Value
        End If
        Ventas.Show()
        Me.Close()
    End Sub
End Class